package com.fsc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.fsc._enum.ReturnEnum;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.vo.*;
import com.fsc.service.ChatFriendService;
import com.fsc.service.ChatRecordService;
import com.fsc.service.GroupMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping("/message/")
public class MessageTipsController {
    @Autowired
    private ChatRecordService chatRecordService;

    @Autowired
    private ChatFriendService chatFriendService;
    @Autowired
    private GroupMemberService groupMemberService;

    /**
     * @return 所有的最新的一条私聊和群组消息
     * 获取所有的最新的一条私聊和群组消息
     */
    @GetMapping("get-all")
    private ResultWrapper<List<MessageTipVO>> getAllMessage() {
        Integer id = StpUtil.getLoginIdAsInt();
        List<PersonContactListVO> ps = chatFriendService.getContractList(id);
        List<GroupListContactVO> gs = groupMemberService.getYourGroupList(id);
        List<MessageTipPlusTimeVO> messageTipVOS = new ArrayList<>();
        for (PersonContactListVO p : ps
        ) {
            MessageTmpVO tmp=chatRecordService.getMsgTmp(id,p.getId());
            if(tmp==null)continue;
            MessageTipPlusTimeVO msg= MessageTipPlusTimeVO.builder()
                    .sendId(p.getId())
                    .chatType(0)
                    .iconUrl(p.getIconUrl())
                    .message(tmp.getMessage())
                    .messageType(tmp.getMessageType())
                    .duration(tmp.getDuration())
                    .time(tmp.getTime())
                    .name(p.getNickname()).build();
            messageTipVOS.add(msg);
        }
        for (GroupListContactVO g:gs
             ) {
            MessageTmpVO tmp=chatRecordService.getMsgTmp(g.getId(),null);
            if(tmp==null)continue;
            MessageTipPlusTimeVO msg=MessageTipPlusTimeVO.builder()
                    .sendId(tmp.getSendId())
                    .chatType(1)
                    .groupId(g.getId())
                    .iconUrl(g.getIconUrl())
                    .message(tmp.getMessage())
                    .messageType(tmp.getMessageType())
                    .duration(tmp.getDuration())
                    .name(g.getGroupName())
                    .time(tmp.getTime())
                    .build();
            messageTipVOS.add(msg);
        }
        messageTipVOS.sort(Comparator.comparing(MessageTipPlusTimeVO::getTime).reversed());
        List<MessageTipVO> messageTipVOs = messageTipVOS.stream()
                .map(msg -> MessageTipVO.builder()
                        .sendId(msg.getSendId())
                        .chatType(msg.getChatType())
                        .groupId(msg.getGroupId())
                        .iconUrl(msg.getIconUrl())
                        .message(msg.getMessage())
                        .messageType(msg.getMessageType())
                        .duration(msg.getDuration())
                        .name(msg.getName())
                        .build())
                .toList();
        return ResultWrapper.success(ReturnEnum.SUCCESS,messageTipVOs);
    }

}
